var t = getApp()
Page({
  data: {
    iconsUrl:t.globalData.iconsUrl,
    userInfo:wx.getStorageSync('userInfo'),
    info:'',
    navHeight:t.globalData.navHeight, // 导航高度
    agree:false,
  },
  onLoad: function (options) {
  
  },
  onShow(e){
    let pages = getCurrentPages()
    console.log(pages.length,pages)
    var that = this
    that.setData({
      userInfo:wx.getStorageSync('userInfo'),
    })
    this.get_info();
  },
  // info
  get_info(e){
    var that=this
    t.request('/users/info',{},function(e){
      console.log(e);
      if (e.code==200) {
        that.setData({
          info:e.result
        })
        if(e.result.cars > 0)
        t.globalData.show_login = false;
      }

    })
  },
  // 同意协议
  checkRadio(e){
    this.setData({
      agree:!this.data.agree
    })
  },
  // 暂不登录按钮(返回首页)
  exitback() {
    let pages = getCurrentPages()
    console.log(pages.length,pages)
    if(pages.length <3){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }else{
      wx.navigateBack({
        delta: 2
      })
    }
  },
  //电话登录
  getPhoneNumber(e){
    if(!this.data.agree){
      wx.showToast({ title: '请同意隐私协议！',icon:'none'})
      return false;
    }
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData;
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    // 请求接口
    wx.login({
      success(e){
        console.log(e);
        wx.showLoading({
          title: '请稍后',
        })
        t.request('/login/getPhoneNumber',{code: e.code,iv:iv,encryptedData:da},function(event){
          wx.hideLoading({})
          if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
          if (event.code==200) {
            wx.setStorageSync('tel', event.result)
            t.globalData.show_tel_login =false
            that.backUrl()
          }
        })
      }
    })
  },
  //菜单跳转
  clickMenu(e){
    let type =  e.currentTarget.dataset.type
    let url = '',tabbarUrl ='';
    switch(type){
      case '1': //用户协议
        url='article/article?id=334'
      break;
      case '2': //隐私协议
        url='article/article?id=335'
      break;
      case '3': //免责声明
        url='article/article?id=336'
      break;
      case '4': //小程序保护指引
        this.handleOpenPrivacyContract()
    break;
      default:
    }
    // 普通页面跳转
    if(url!=""){
      wx.navigateTo({
        url: '/pages/'+url,
      })
    }
    // tabbar页面跳转
    if(tabbarUrl!=""){
      wx.switchTab({
        url: '/pages/'+tabbarUrl,
      })
    }
  },
  // 返回上一页
  backUrl(e){
    let pages = getCurrentPages()
    console.log(pages.length,pages)
    if(pages.length <2){
      wx.switchTab({
        url: '/pages/index/index',
      })
    }else{
      wx.navigateBack({
        delta: 1
      })
    }
  },
   // 打开隐私协议页面
  handleOpenPrivacyContract() {
    // 打开隐私协议页面
    wx.openPrivacyContract({
      success: () => {}, // 打开成功
      fail: () => {}, // 打开失败
      complete: () => {}
    })
  }
})